package com.ztgf.commodity.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.commodity.models.bo.CommodityParamValueBO;
import com.ztgf.commodity.models.bo.CommodityParamValueSaveBO;
import com.ztgf.commodity.models.dto.CommodityParamValueDTO;
import com.ztgf.commodity.models.service.ICommodityParamValueService;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * 商品参数值表控制器
 *
 * @author chenyu.wang
 * @Date 2020-06-15 10:13:11
 */

@RestController
@RequestMapping("/cms/commodityParamValue")
@Slf4j
@Api(value = "CommodityParamValueController", tags = "商品参数值表接口")
public class CommodityParamValueController {

    @DubboReference
    private ICommodityParamValueService commodityParamValueService;

    @ApiOperation(value = "添加商品参数值表", notes = "添加商品参数值表")
    @PostMapping(value = "/insert")
    public Result insert(CommodityParamValueSaveBO saveBO) {
        boolean flag = commodityParamValueService.add(saveBO);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存商品参数值表", notes = "保存商品参数值表")
    public Result save(CommodityParamValueSaveBO saveBO) {
        if (saveBO.getId() == null) {
            //添加
            return insert(saveBO);
        } else {
            return update(saveBO);
        }
    }

    @ApiOperation(value = "修改商品参数值表", notes = "修改商品参数值表")
    @PostMapping("/update")
    public Result update(CommodityParamValueSaveBO saveBO) {
        CommodityParamValueDTO dto = new CommodityParamValueDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = commodityParamValueService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }


    @ApiOperation(value = "删除商品参数值表", notes = "删除商品参数值表")
    @ApiImplicitParam(name = "id", value = "商品参数值表id", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id) {
        boolean flag = commodityParamValueService.deleteById(id);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除商品参数值表", notes = "批量删除商品参数值表")
    @ApiImplicitParam(name = "ids", value = "ids:以逗号隔开", required = true, paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids) {
        if (StrUtils.isEmpty(ids)) {
            return new Result(CodeEnum.FAIL_100002);
        }
        String[] arr = ids.split(",");
        commodityParamValueService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找", notes = "根据id查找")
    @ApiImplicitParam(name = "id", value = "id", required = true, paramType = "query", dataType = "Long")
    public Result<CommodityParamValueDTO> get(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }

        CommodityParamValueDTO dto = commodityParamValueService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "商品参数值表列表", notes = "商品参数值表列表")
    @PostMapping("/queryPageList")
    public Result<Page<CommodityParamValueDTO>> queryPageList(CommodityParamValueBO bo) {
        Wrapper<CommodityParamValueDTO> wrapper = new EntityWrapper<>();
        Page<CommodityParamValueDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = commodityParamValueService.selectPage(page, wrapper);
        return new Result<>(page);
    }
}
